package org.lzh.controller;

import org.lzh.pojo.Answer;
import org.lzh.service.AnswerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

//学生答案管理类
@Controller
@RequestMapping("answer")
public class AnswerController {
    @Autowired
    private AnswerService answerService;





    //    ---------------------------------------------------------------新增操作-----------------------------------------------------------


    //添加问题
    @RequestMapping(value = "/addAnswer", method = RequestMethod.POST)

    public String login(
            @ModelAttribute Answer answer,//@ModelAttribute这个注解可以将注册时输入的值封装成user对象，很方便
//            @RequestBody Answer answer,
            Model model             //model可以用model.addAttribute 添加属性，然后在跳转的jsp页面里用，也很好用
    ) throws IOException {
        //判断answer数据库里是不是已经交过答案了


        if(answerService.findByStudentId(answer.getStudentId())!=null&&answerService.findByStudentId(answer.getStudentId()).getState()==0){//这个人的进入考试没有交过卷，就进行交卷
            answerService.updateByStuidAndTestidDynamic(answer);
            model.addAttribute("tip", "交卷成功");
        }
        model.addAttribute("alreadyTest","您已经做过试卷了，请耐心老师批改试卷");
        return "student" ;


    }







    //    ---------------------------------------------------------------新增操作-----------------------------------------------------------


    //根据学生id查找答案
    public Answer findByStudentId(String studentId){
        return answerService.findByStudentId(studentId);
    };
}
